#include <iostream>
#include <memory>
using namespace std;
#include <cstring>
#include <unistd.h>
#include "Util.hpp"
#include "Task.hpp"

int main()
{
    Util::setNonBlock(0);
    std::unique_ptr<Task> task(new Task());
    task->loadTask();
    char buffer[1024] = {0};
    while (true)
    {
        int n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n - 1] = '\0';
            cout << "echo# " << buffer << endl;
        }
        else if (n == 0)
        {
            cout << "read done..." << endl;
            break;
        }
        else
        {
            if (errno == EAGAIN || errno == EWOULDBLOCK)
            {
                // cout << "read later... : " << strerror(errno) << endl;
                task->runTask();
                sleep(1);
                continue;
            }
            else if(errno == EINTR)
            {
                cout << "read interrupted... : " << strerror(errno) << endl;
                // sleep(1);
                continue;
            }
            else
            {
                cout << "read error: " << strerror(errno) << endl;
                break;
            }
        }
    }

    return 0;
}